package com.crm.system.service.impl.userForumManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.DhPageInfo;
import com.crm.common.util.CommonUtils;
import com.crm.common.util.JSONUtility;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.entity.userForumManager.UserForumReply;
import com.crm.model.vo.customerManager.AnalyseCustomerRedisVO;
import com.crm.model.vo.userForumManager.UserForumReplyVO;
import com.crm.model.vo.userForumManager.UserForumSonReplyVO;
import com.crm.model.vo.userForumManager.UserForumVO;
import com.crm.service.userForumManager.UserForumReplyDbService;
import com.crm.system.service.userForumManager.UserForumReplyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/5/24 9:37
 */
@Service
public class UserForumReplyServiceImpl implements UserForumReplyService {

    @Autowired
    private UserForumReplyDbService userForumReplyDbService;

    /**
     * 添加评论
     * @param userForumReply
     */
    @Override
    public UserForumReplyVO addForumReply(UserForumReply userForumReply) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//当前登录人信息
        userForumReply.setUserForumReplyId(CommonUtils.getGUID());
        userForumReply.setCreatePeopleId(userAccount.getAccountId());
        userForumReply.setCreateTm(new Date());
        boolean b =userForumReplyDbService.addForumReply(userForumReply);
        if (b){
            UserForumReplyVO vo;
            vo = userForumReplyDbService.selectForumReplyById(userForumReply.getUserForumReplyId());
            return vo;
        }
        return null;
    }

    /**
     * 查看评论
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param userForumId
     * @return
     */
    @Override
    public DhPageInfo<UserForumReplyVO> selectForumReply(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String userForumId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }
        List<UserForumReplyVO> forumList = userForumReplyDbService.selectForumReplyByLevel(1, order.toString(),null,null,userForumId);
        for (int i=0;i<forumList.size();i++){
           // forumList.get(i).setSonReplyList(userForumReplyDbService.selectForumReplyByPid(2,null,2,forumList.get(i).getUserForumReplyId(),userForumId));
            //不限制子评论数量
            forumList.get(i).setSonReplyList(userForumReplyDbService.selectForumReplyByLevel(2,null,5,forumList.get(i).getUserForumReplyId(),userForumId));
        }
        //加阅读次数
        userForumReplyDbService.addReadNumberByUserForumId(userForumId);
        PageInfo<UserForumReplyVO> pageInfo = new PageInfo<UserForumReplyVO>(forumList);
        return new DhPageInfo<UserForumReplyVO>(pageInfo, forumList);
    }

    /**
     * 查看子评论
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param userForumReplyId
     * @return
     */
    @Override
    public DhPageInfo<UserForumReplyVO> selectForumSonReply(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String userForumReplyId,String userForumId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }
        //List<UserForumReplyVO> forumList = userForumReplyDbService.selectForumSonReply(userForumReplyId, order.toString());
        List<UserForumReplyVO> forumList = userForumReplyDbService.selectForumReplyByLevel(2,null,null,userForumReplyId,userForumId);
        UserForumReplyVO vo = new UserForumReplyVO();
        if (forumList.size() > 0){
            vo = userForumReplyDbService.selectForumReplyById(forumList.get(0).getOneReplyId());
            vo.setSonReplyList(forumList);
            //加阅读次数
            userForumReplyDbService.addReadNumberByUserForumId(forumList.get(0).getForumId());
        }
        PageInfo<UserForumReplyVO> pageInfo = new PageInfo<UserForumReplyVO>(forumList);
        return new DhPageInfo<UserForumReplyVO>(pageInfo, forumList);
    }

    /**
     * 根据id获取评论信息
     * @param oneReplyId
     * @return
     */
    @Override
    public UserForumSonReplyVO selectForumReplyById(String oneReplyId) {
        UserForumReplyVO vo = userForumReplyDbService.selectForumReplyById(oneReplyId);
        String json = JSONObject.toJSONString(vo);
        UserForumSonReplyVO result = new UserForumSonReplyVO();
        if (Objects.nonNull(json)){
            JSONObject jsonObject = JSONObject.parseObject(json);
            result = JSONObject.toJavaObject(jsonObject, UserForumSonReplyVO.class);
        }
        return result;
    }

    /**
     * 删除评论
     * @param userForumReplyId
     * @return
     */
    @Override
    public boolean delForumReply(String userForumReplyId) {
        return userForumReplyDbService.delForumReply(userForumReplyId);
    }
}
